package com.xiaoshuidi.cloud.module.report.service.datamanage;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.contract.enums.contract.IdTypeEnum;
import com.xiaoshuidi.cloud.module.report.controller.datamanage.vo.AgeSexDistributionRespVO;
import com.xiaoshuidi.cloud.module.report.controller.datamanage.vo.RentDistributionRespVO;
import com.xiaoshuidi.cloud.module.report.dal.mapper.datamanage.UserPortraitMapper;
import com.xiaoshuidi.cloud.module.report.utils.ApartmentUtils;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserPortraitServiceImpl implements UserPortraitService {
    @Resource
    private UserPortraitMapper userPortraitMapper;
    @Resource
    private ApartmentUtils apartmentUtils;

    /**
     * 在租租户租金分布
     *
     * @param apartmentIds       门店id
     * @param includCohabitants 是否包含同住人
     * @return
     */
    @Override
    public RentDistributionRespVO rentDistribution(List<Long> apartmentIds, Boolean includCohabitants) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        RentDistributionRespVO rentDistributionRespVO = userPortraitMapper.rentDistribution(apartmentIds, includCohabitants);
        // 计算所占比例
        if (ObjectUtil.isNotEmpty(rentDistributionRespVO.getTotalCount()) && rentDistributionRespVO.getTotalCount() > 0) {
            BigDecimal totalCount = BigDecimal.valueOf(rentDistributionRespVO.getTotalCount());
            rentDistributionRespVO.setFirstStepRate(getRate4(rentDistributionRespVO.getFirstStep(), totalCount))
                    .setSecondStepRate(getRate4(rentDistributionRespVO.getSecondStep(), totalCount))
                    .setThirdStepRate(getRate4(rentDistributionRespVO.getThirdStep(), totalCount))
                    .setFourthStepRate(getRate4(rentDistributionRespVO.getFourthStep(), totalCount))
                    .setFifthStepRate(getRate4(rentDistributionRespVO.getFifthStep(), totalCount));
        }
        return rentDistributionRespVO;
    }

    private BigDecimal getRate4(Long quantity, BigDecimal totalCount) {
        return BigDecimal.valueOf(quantity).divide(totalCount, 4, RoundingMode.HALF_UP);
    }

    /**
     * 在租租户年龄、性别分布
     * @param apartmentIds 门店id
     * @param includCohabitants 是否包含同住人
     * @return
     */
    @Override
    public AgeSexDistributionRespVO ageSexDistribution(List<Long> apartmentIds, Boolean includCohabitants) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        AgeSexDistributionRespVO ageSexDistributionRespVO = userPortraitMapper.ageSexDistribution(apartmentIds, includCohabitants);
        if(ObjectUtil.isNotEmpty(ageSexDistributionRespVO.getTotalCount()) && ageSexDistributionRespVO.getTotalCount() > 0){
            BigDecimal totalCount = BigDecimal.valueOf(ageSexDistributionRespVO.getTotalCount());
            ageSexDistributionRespVO
                    .setManRate(getRate4(ageSexDistributionRespVO.getMan(), totalCount))
                    .setWomanRate(getRate4(ageSexDistributionRespVO.getWoman(), totalCount));
        }
        BigDecimal totalCount = BigDecimal.valueOf(ageSexDistributionRespVO.getFirstStep()+
                ageSexDistributionRespVO.getSecondStep()+
                ageSexDistributionRespVO.getThirdStep()+
                ageSexDistributionRespVO.getFourthStep()+
                ageSexDistributionRespVO.getFifthStep()
//                + ageSexDistributionRespVO.getOtherStep()
                )
                ;
        if(ObjectUtil.isNotEmpty(totalCount) && totalCount.longValue() > 0){
        ageSexDistributionRespVO.setFirstStepRate(getRate4(ageSexDistributionRespVO.getFirstStep(), totalCount))
                .setSecondStepRate(getRate4(ageSexDistributionRespVO.getSecondStep(), totalCount))
                .setThirdStepRate(getRate4(ageSexDistributionRespVO.getThirdStep(), totalCount))
                .setFourthStepRate(getRate4(ageSexDistributionRespVO.getFourthStep(), totalCount))
                .setFifthStepRate(getRate4(ageSexDistributionRespVO.getFifthStep(), totalCount))
//                .setOtherStepRate(getRate4(ageSexDistributionRespVO.getOtherStep(), totalCount))
        ;
        }
        return ageSexDistributionRespVO;
    }

    /**
     * 在租租客地域分布
     * @param apartmentIds 门店id
     * @param includCohabitants 是否包含同住人
     * @return
     */
    @Override
    public Map<String, Long> regionDistribution(List<Long> apartmentIds, Boolean includCohabitants) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        List<Map<String, Long>> maps = userPortraitMapper.regionDistribution(apartmentIds, includCohabitants);
        Map<String, Long> respMap = new HashMap<>();
        log.info(JsonUtils.toJsonString(maps));
        for (Map<String, Long> map : maps) {
            String card = StrUtil.fillAfter(StrUtil.toString(map.get("card_no")), '0', 18);
            String province = IdcardUtil.getProvinceByIdCard(card);
            if(ObjectUtil.isNotEmpty(province)){
                respMap.put(province, map.get("num"));
            }
        }
        return respMap;
    }

    /**
     * 在租租客证件类型分布
     * @param apartmentIds 门店id
     * @param includCohabitants 是否包含同住人
     * @return
     */
    @Override
    public Map<String, Long> cardTypeDistribution(List<Long> apartmentIds, Boolean includCohabitants) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        List<Map<String, Long>> maps = userPortraitMapper.cardTypeDistribution(apartmentIds, includCohabitants);
        log.info(JsonUtils.toJsonString(maps));
        Map<String, Long> respMap = new HashMap<>();
        for (Map<String, Long> map : maps) {
            String name = IdTypeEnum.getName(StrUtil.toString(map.get("card_type")));
            if(ObjectUtil.isNotEmpty(name)){
                respMap.put(name, map.get("num"));
            }
        }
        return respMap;
    }


    @NotNull
    private BigDecimal getRate(Long quantity, BigDecimal totalCount) {
        return BigDecimal.valueOf(quantity).divide(totalCount, 2, RoundingMode.HALF_UP);
    }

}
